/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock;

import java.lang.Thread.UncaughtExceptionHandler;

import com.flan.stock.activity.BeginActivity;
import com.flan.stock.utils.Log;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;

/**
 * 自定义全局未捕获异常处理
 * @author flan
 * @date   2015年11月17日
 */
public class StockGlobalExceptionHandler implements UncaughtExceptionHandler {
	
	private static final String TAG = "StockUncaughtExceptionHandler";
	
	private Application application;
	
	//系统默认的UncaughtException处理类
	private Thread.UncaughtExceptionHandler defaultExcepthinHandler;
	
	private static StockGlobalExceptionHandler globalExceptionHandler;
	
	private StockGlobalExceptionHandler() {}
	
	/**
	 * 单例模式创建全局异常处理对象
	 * @return
	 */
	public static StockGlobalExceptionHandler getInstance(){
		if(globalExceptionHandler == null){
			globalExceptionHandler = new StockGlobalExceptionHandler();
			return globalExceptionHandler;
		}
		return globalExceptionHandler;
	}
	
	public void init(Application application){
		this.application = application;
		
		//获取系统默认的UncaughtException处理器
		defaultExcepthinHandler = Thread.getDefaultUncaughtExceptionHandler();
		//为系统设置自定义的全局异常处理器
		Thread.setDefaultUncaughtExceptionHandler(this);
		Log.d(TAG,"全局异常处理器初始化完毕！");
	}
	
	/**
	 * 出现为捕获的异常时会进入该方法中进行处理
	 */
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		if(!handlerException(ex) && defaultExcepthinHandler != null){
			//如果自定义的全局异常处理器不处理，则交给系统处理
			Log.d(TAG, "全局异常处理器无法处理未捕获异常，交由系统处理！");
			defaultExcepthinHandler.uncaughtException(thread, ex);
		}
	}
	
	/**
	 * 全局异常处理逻辑
	 * @param ex
	 * @return	true 该方法已处理了异常，false 该方法未处理异常
	 */
	private boolean handlerException(Throwable ex){
		
		if(ex == null){
			return false;
		}
        
        Intent intent = new Intent(application.getApplicationContext(),BeginActivity.class);  
        PendingIntent restartIntent = PendingIntent.getActivity(    
        		application.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                 
        //退出程序                                          
        AlarmManager mgr = (AlarmManager) application.getSystemService(application.ALARM_SERVICE);    
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, restartIntent); // 1秒钟后重启应用   
        android.os.Process.killProcess(android.os.Process.myPid());  
        System.exit(1);
		
		Log.d(TAG, "//////////////////////////// 异常我已经处理了 ////////////////////");
		return true;
	}

}
